package pro.shushi.pamirs.hr.core.action.eam;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import pro.shushi.pamirs.hr.api.enums.BooleanConstantEnum;
import pro.shushi.pamirs.hr.api.enums.eam.EamExpEnum;
import pro.shushi.pamirs.hr.api.model.eam.BsMtool;
import pro.shushi.pamirs.hr.api.model.eam.BsMtoolMd;
import pro.shushi.pamirs.meta.annotation.Action;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.Model;
import pro.shushi.pamirs.meta.annotation.fun.extern.Slf4j;
import pro.shushi.pamirs.meta.common.exception.PamirsException;
import pro.shushi.pamirs.meta.constant.ExpConstants;
import pro.shushi.pamirs.meta.constant.FunctionConstants;
import pro.shushi.pamirs.meta.enmu.FunctionOpenEnum;
import pro.shushi.pamirs.meta.enmu.FunctionTypeEnum;
import pro.shushi.pamirs.meta.enmu.ViewTypeEnum;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 工装型号自定义
 */
@Slf4j
@Component
@Model.model(BsMtool.MODEL_MODEL)
public class BsMtoolAction {

    /**
     * 自定义构造方法示例
     * 如：界面设计器提交数据-选择的提交函数，新增时系统会调用construct
     * @param data
     * @return
     */
    @Function(openLevel = FunctionOpenEnum.API)
    @Function.Advanced(type = FunctionTypeEnum.QUERY, displayName = "复制")
    public BsMtool copy(BsMtool data) {
        if (data.getId() != null) {
            data = data.queryById();
            data.unsetId();
            data.unsetCreateDate();
            data.unsetWriteDate();
            data.unsetCreateUid();
            data.unsetWriteUid();
            data.unsetMtoolCode();
        }

        return data;
    }

    @Action.Advanced(name = FunctionConstants.create, managed = true, invisible = ExpConstants.idValueExist)
    @Action(displayName = "保存", summary = "添加", bindingType = ViewTypeEnum.FORM)
    @Function(name = FunctionConstants.create)
    @Function.fun(FunctionConstants.create)
    public BsMtool create(BsMtool data) {

        this.checkAddOrEdit(data);

        data.construct();
        data.create();
        return data;
    }

    // 重写默认的更新保存函数(如果默认满足则不需要重写)
    @Transactional
    @Function.Advanced(type = FunctionTypeEnum.UPDATE)
    @Action.Advanced(name = FunctionConstants.update, managed = true, invisible = ExpConstants.idValueNotExist)
    @Action(displayName = "更新", summary = "修改", bindingType = ViewTypeEnum.FORM)
    @Function(name = FunctionConstants.update)
    @Function.fun(FunctionConstants.update)
    public BsMtool update(BsMtool data) {

        this.checkAddOrEdit(data);

        data.updateById();
        return data;
    }

    /**
     * 校验工装特殊分类与保养项
     */
    private void checkAddOrEdit(BsMtool data){
        //校验工装型号存在且启用
        String orgCode = data.getOrgCode();
        String mmdCode = data.getMmdCode();
        BsMtoolMd mdRow = new BsMtoolMd().setOrgCode(orgCode).setMmdCode(mmdCode).queryOne();
        if (mdRow == null) {
            //工装型号：{}在组织编码：{}中未定义。
            throw PamirsException.construct(EamExpEnum.MTOOLMD_NOT_EXISTS,orgCode,mmdCode).errThrow();
        }
        if(!BooleanConstantEnum.Y.value().equals(mdRow.getIsValid().getValue())){
            //工装型号：{}在组织编码：{}中未启用。
            throw PamirsException.construct(EamExpEnum.MTOOLMD_NOT_VALID,orgCode,mmdCode).errThrow();
        }

        //校验时间
        Date now = new Date();
        Date inDate = data.getInDate();
        Date mfDate = data.getMfDate();
        Date purDate = data.getPurDate();
        boolean inDateFlag = inDate != null;
        boolean mfDateFlag = mfDate != null;
        boolean purDateFlag = purDate != null;

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(mfDateFlag){
            if(mfDate.after(now)){
                //{} 值不正确，必须小于：{}。
                throw PamirsException.construct(EamExpEnum.MUST_LESS_ERROR,"生产日期："+df.format(mfDate),"当前时间").errThrow();
            }
        }
        if(purDateFlag){
            if(purDate.after(now)){
                //{} 值不正确，必须小于：{}。
                throw PamirsException.construct(EamExpEnum.MUST_LESS_ERROR,"购买日期："+df.format(purDate),"当前时间").errThrow();
            }
        }
        if(inDateFlag){
            if(inDate.after(now)){
                //{} 值不正确，必须小于：{}。
                throw PamirsException.construct(EamExpEnum.MUST_LESS_ERROR,"入库日期："+df.format(inDate),"当前时间").errThrow();
            }
            if(mfDateFlag && mfDate.after(inDate)){
                //{} 值不正确，必须小于：{}。
                throw PamirsException.construct(EamExpEnum.MUST_LESS_ERROR,"生产日期："+df.format(mfDate),"入库日期："+df.format(inDate)).errThrow();
            }
            if(purDateFlag && purDate.after(inDate)){
                //{} 值不正确，必须小于：{}。
                throw PamirsException.construct(EamExpEnum.MUST_LESS_ERROR,"购买日期："+df.format(purDate),"入库日期："+df.format(inDate)).errThrow();
            }
        }





    }




}
